package org.darkforest.merchant.controller;

import org.apache.commons.lang.StringUtils;
import org.darkforest.annotation.ArchivesLog;
import org.darkforest.common.logger.Logger;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.Utils;
import org.darkforest.dao.mapper.ActiveAreaGoodsMapper;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.enums.AjaxMsgTypeEnum;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.ActiveAreaGoods;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.GoodsCommonSearchBean;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 特色专区推荐商品控制类
 *
 * @auth bingo
 * @create 2017-11-22-18:27
 */
@Controller
@RequestMapping(value = "activeArea")
public class ActiveAreaController {


    @Autowired
    private GoodsCommonMapper goodsCommonMapper;

    @Autowired
    private ActiveAreaGoodsMapper activeAreaGoodsMapper;


    @Autowired
    private IGoodsService goodsService;
    @RequestMapping(value = "activeAreaGoodsList")
    public ModelAndView list(HttpServletRequest request, @RequestParam(defaultValue = "1", required = false) Integer p) {
        ModelAndView res = new ModelAndView("promotion/active_area_goods");
        //得到店铺编号storeId
        StoreSellerVo sellerVo = (StoreSellerVo) request.getSession().getAttribute("sellerInfo");
        //查询条件
        String goodsName = request.getParameter("goodsName");
        String recommendType = request.getParameter("recommendType");
        ActiveAreaGoods activeAreaGoods = new ActiveAreaGoods();
        activeAreaGoods.setStart(p);
        activeAreaGoods.setStoreId(sellerVo.getStoreId());
        activeAreaGoods.setRecommendType(StringUtils.isBlank(recommendType)?null:recommendType);
        activeAreaGoods.setGoodsName(goodsName);
        List<ActiveAreaGoods> activeAreaGoodsList = activeAreaGoodsMapper.selectByCondition(activeAreaGoods);
        activeAreaGoods.setResultList(activeAreaGoodsList);
        res.addObject("pageBean", activeAreaGoods);
        return res;
    }

    /**
     * 删除专区商品
     *
     * @param rId
     * @return
     */
    @ArchivesLog(operationDescription = "删除特色专区商品")
    @RequestMapping(value = "delActiveAreaGoods")
    public @ResponseBody AjaxReturnData delActiveAreaGoods(int rId) {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        ajaxReturnData.setUrl("activeArea/activeAreaGoodsList");
        int i = activeAreaGoodsMapper.deleteByPrimaryKey(rId);
        if(i==1){
            ajaxReturnData.setMessage("商品删除成功");
            ajaxReturnData.setType(AjaxMsgTypeEnum.SUCC.getText());
        }else{
            ajaxReturnData.setMessage("商品删除失败");
            ajaxReturnData.setType(AjaxMsgTypeEnum.INFO.getText());
        }

        return ajaxReturnData;

    }


    /**
     * 新增特色专区商品
     *
     * @param goodsCommonid
     * @return
     */
    @ArchivesLog(operationDescription = "新增特色专区商品")
    @RequestMapping(value = "addActiveAreaGoods")
    public
    @ResponseBody
    AjaxReturnData addActiveAreaGoods(int goodsCommonid, String activeAreaType, Integer sort) {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        Assert.notNull(goodsCommonid, "goodsCommonid can not be none");
        Assert.notNull(activeAreaType, "activeAreaType can not be none");


        List<ActiveAreaGoods> activeAreaGoodses = activeAreaGoodsMapper.selectByCondition(new ActiveAreaGoods(goodsCommonid));

        if (activeAreaGoodses.size() > 0) {
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("您已经推荐此商品，请选择其他商品");

        } else {
            GoodsCommon goodsCommon = goodsCommonMapper.selectByPrimaryKey(goodsCommonid);
            ActiveAreaGoods activeAreaGoods = new ActiveAreaGoods();
            activeAreaGoods.setState("1");
            activeAreaGoods.setSort(sort);
            activeAreaGoods.setDisplay("-1");
            activeAreaGoods.setGoodsCommonid(goodsCommon.getGoodsCommonid());
            activeAreaGoods.setGoodsJingo(goodsCommon.getGoodsJingle());
            activeAreaGoods.setCreateTime(DateUtil.getCurrentDateTime());
            activeAreaGoods.setGoodsImage(goodsCommon.getGoodsImage());
            activeAreaGoods.setGoodsName(goodsCommon.getGoodsName());
            activeAreaGoods.setGoodsPrice(goodsCommon.getGoodsPrice());
            activeAreaGoods.setRecommendType(activeAreaType);
            activeAreaGoods.setStoreId(goodsCommon.getStoreId());
            activeAreaGoods.setStoreName(goodsCommon.getStoreName());
            activeAreaGoodsMapper.insertSelective(activeAreaGoods);
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("操作成功");
        }


        return ajaxReturnData;
    }


    /**
     * 选择推荐商品列表
     * @param goodsName
     * @param start
     * @return
     */
    @RequestMapping(value = "chooseActiveAreaGoods")
    public ModelAndView chooseActiveAreaGoods(HttpServletRequest request,
                                             @RequestParam(required = false, defaultValue = "") String goodsName,
                                             @RequestParam(required = false, defaultValue = "1") String start){
        ModelAndView modelAndView = new ModelAndView("promotion/active_area_goods_search");
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        GoodsCommonSearchBean goodSearchBean = new GoodsCommonSearchBean();
        //goodSearchBean.setGoodsName(Utils.encodeGetParam(goodsName));
        goodSearchBean.setSearchColumn("goods_name");
        goodSearchBean.setSearchKey(goodsName);
        goodSearchBean.setStoreId(sellerVo.getStoreId());
        goodSearchBean.setStart(Integer.parseInt(start));
        goodSearchBean.setGoodsType(0);
        goodSearchBean.setInStatus("(1)");
        goodSearchBean.setGoodsVerify(1);
        this.goodsService.findGoodsInState(goodSearchBean);

        modelAndView.addObject("pageBean",goodSearchBean);
        return modelAndView;
    }
}
